﻿using System;
using System.Linq;
using BestEasyCMS.Framework.Querying;
using BestEasyCMS.Framework.UnitOfWork;
using BestEasyCMS.Model.IRepository.Security;
using BestEasyCMS.Model.Security;
using BestEasyCMS.Service.Interfaces.Security;
using BestEasyCMS.Service.Mappers;
using BestEasyCMS.Service.Messages;
using BestEasyCMS.Service.Messages.Security.Request;
using BestEasyCMS.Service.Views.Security;

namespace BestEasyCMS.Service.Impl.Security
{
    public class RoleService : IRoleService
    {
        private readonly IRoleRepository _roleRepository;
        private readonly IUnitOfWork _uow;
        public RoleService(IRoleRepository roleRepository, IUnitOfWork uow)
        {
            _roleRepository = roleRepository;
            _uow = uow;
        }

        public FindResponse<RoleView> FindBy(long id)
        {
            var response = new FindResponse<RoleView>();
            var result = _roleRepository.FindBy(id);
            if (result != null)
            {
                response.View = Convert<RoleView, Role>.ToView(result);
                response.IsSuccess = true;
                response.Message = "获取成功";
            }
            else
            {
                response.View = null;
                response.IsSuccess = false;
                response.Message = "获取不到相关数据！";
            }
            return response;
        }

        public ResponseBase Update(RoleView view)
        {
            var response = new ResponseBase();
            try
            {
                _roleRepository.Update(Convert<RoleView, Role>.ToEntity(view));
                _uow.Commit();
                response.IsSuccess = true;
                response.Message = "更新成功！";
            }
            catch (Exception ex)
            {
                response.IsSuccess = false;
                response.Message = ex.Message;
            }
            return response;
        }

        public ResponseBase Add(RoleView view)
        {
            var response = new ResponseBase();
            try
            {
                _roleRepository.Insert(Convert<RoleView, Role>.ToEntity(view));
                _uow.Commit();
                response.IsSuccess = true;
                response.Message = "新增成功！";
            }
            catch (Exception ex)
            {
                response.IsSuccess = false;
                response.Message = ex.Message;
            }
            return response;
        }

        public FindPageResponse<RoleView> GetRolesPageBy(GetRolesPageByRequest request)
        {
            var response = new FindPageResponse<RoleView>();

            var results = _roleRepository.Query()
                .ToPage(request.Paged.PageIndex, request.Paged.PageSize);

            if (!results.Items.Any())
            {
                response.IsSuccess = false;
                response.Message = "暂无相关数据！";
                return response;
            }
            response.IsSuccess = true;
            response.Message = "获取成功！";
            response.Page = results.ToPageView<RoleView, Role>();
            return response;
        }


        public ResponseBase Delete(long id)
        {
            var response = new ResponseBase();
            //var userView = _userRepository.DeleteMany().Where(p => p.RoleId == id).Execute();
            return response;
        }
    }
}
